/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.client.core.modal.grr;

import com.google.common.collect.Lists;
import com.intelligent.ispc.client.utils.SystemConfigUtils;
import com.intelligent.ispc.client.utils.UIConstant;
import com.intelligent.ispc.common.utils.StringUtils;
import com.intelligent.ispc.core.dto.GrrAnovaDto;

import javax.swing.table.AbstractTableModel;
import java.util.Arrays;
import java.util.List;

/**
 * Created by cherry on 2016/8/29.
 */
public class AnovaTableModel extends AbstractTableModel {

    private String[] columns;
    private List<GrrAnovaDto> data;
    private int digGrrNum = UIConstant.DEFAULT_GRR_DIGNUM;

    @Override
    public int getRowCount() {
        if (data == null) {
            return 0;
        } else {
            return data.size();
        }
    }

    @Override
    public String getColumnName(int column) {

        if (columns == null) {
            return "";
        }
        return columns[column];
    }

    @Override
    public int getColumnCount() {
        if (columns == null) {
            return 0;
        } else {
            return columns.length;
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (data == null || data.size() <= rowIndex) {
            return "";
        }
        GrrAnovaDto grrAnova = data.get(rowIndex);
        String keyValue = grrAnova.getKeyValue();
        String dfValue = grrAnova.getDfValue();
        String ssValue = grrAnova.getSsValue();
        String msValue = grrAnova.getMsValue();
        String fValue = grrAnova.getfValue();
        String probFValue = grrAnova.getProbFValue();

        switch (columnIndex) {
            case 0:
                return StringUtils.isBlank(keyValue) ? "-" : keyValue;
            case 1:
                return StringUtils.isNumeric(dfValue) ? StringUtils.formatDouble(Double.valueOf(dfValue), digGrrNum) : "-";
            case 2:
                return StringUtils.isNumeric(ssValue) ? StringUtils.formatDouble(Double.valueOf(ssValue), digGrrNum) : "-";
            case 3:
                return StringUtils.isNumeric(msValue) ? StringUtils.formatDouble(Double.valueOf(msValue), digGrrNum) : "-";
            case 4:
                return StringUtils.isNumeric(fValue) ? StringUtils.formatDouble(Double.valueOf(fValue), digGrrNum) : "-";
            case 5:
                return StringUtils.isNumeric(probFValue) ? StringUtils.formatDouble(Double.valueOf(probFValue), digGrrNum) : "-";
            default:
                return "";
        }
    }

    /**
     * anova table model constructor no parameter
     */
    public AnovaTableModel() {

    }

    /**
     * anova table model constructor one parameter
     *
     * @param columns table columns
     */
    public AnovaTableModel(String[] columns) {

        if (columns == null) {
            this.columns = new String[0];
            return;
        }

        this.columns = new String[columns.length];
        System.arraycopy(columns, 0, this.columns, 0, columns.length);
        data = Lists.newArrayList();
    }

    /**
     * set table data source
     *
     * @param dtos anova table data source
     */
    public void setData(List<GrrAnovaDto> dtos) {

        if (data == null) {
            return;
        }

        if (dtos == null) {
            data = Lists.newArrayList();
        } else {
            data.clear();
            data.addAll(dtos);
        }
        fireTableDataChanged();
    }

    public List<GrrAnovaDto> getData() {
        return data;
    }

    /**
     * init grr digNum
     */
    public void initDigGrrNum() {
        digGrrNum = SystemConfigUtils.getInstance().getGrrDigit();
    }
}
